const morgan = require('morgan');
const {ANSI_COLOR, colorizeText} = require('./ANSIColor');

morgan.token('status_color', (req, res, args) => {
  // get the status code if response written
  var status = (typeof res.headersSent !== 'boolean' ? Boolean(res.header) : res.headersSent)
    ? res.statusCode
    : undefined

  // get status color
  var color = status >= 500 ? ANSI_COLOR.red // red
    : status >= 400 ? ANSI_COLOR.yellow // yellow
      : status >= 300 ? ANSI_COLOR.cyan // cyan
        : status >= 200 ? ANSI_COLOR.green // green
          : ANSI_COLOR.reset; // reset

  return colorizeText(status, color);
});

module.exports = morgan;